<?php

function noPageFoundException($class_name,$message){
   eval('class '.$class_name.'{}');
   throw new Exception ($message);
}

function init_db() {
    require PATH_ROOT."lib/adodb5/adodb.inc.php";
    //error_reporting(E_ALL); //Enable reporting of all errors by PHP 
    require PATH_ROOT."lib/adodb5/adodb-errorhandler.inc.php";    
    
    define('ADODB_ERROR_LOG_TYPE', 3);
    define('ADODB_ERROR_LOG_DEST', PATH_ROOT."files/logs/uzytkownik_DB_".$_SERVER['REMOTE_ADDR'].".log");
    
    $ADODB_CACHE_DIR = PATH_ROOT.'files/adodb_cache';
    $db = ADONewConnection('oci8otago');
    $db->charSet = 'UTF8';
    $db->NLS_DATE_FORMAT = 'RRRR-MM-DD HH24:MI:SS';    
    //$db->dateformat = 'RRRR-MM-DD HH24:MI:SS';
    $db->datetime = true;
    if (!$db->PConnect(DB_HOST, DB_USER, DB_PASS, DB_SID)) {
        die("Brak polaczenia z baza danych");
    }
    
    $db->SetFetchMode(ADODB_FETCH_ASSOC);
    //$db->debug = true;
    
    Register::set("DB", $db);
    
    //$db->LogSQL(true); // nie działa
    //$perf =&NewPerfMonitor($db);
    //$perf->UI($pollsecs=5);
    
    return $db;
}

function init_smarty() {
    require PATH_ROOT."includes/smarty/libs/Smarty.class.php";
    $s = new Smarty();
    $s->template_dir = PATH_ROOT.'view/';
    $s->compile_dir = PATH_ROOT.'includes/smarty/compiled/';
    $s->cache_dir = PATH_ROOT.'includes/smarty/cache/';
    //$s->assign('URL',URL);
	//$s->auto_literal = false;
	//daboo $s->compile_check = KOMPILUJ_TPL;
    Register::set("Smarty", $s);
    return $s;
}

function decamelize($CamelCaseVar) {
    $CamelCaseVar{0} = strtolower($CamelCaseVar{0});
    return strtolower(preg_replace('/([A-Z])/', '_\1', $CamelCaseVar));
}

function camelize($not_cc_var) {
    $CamelCase = '';
    $parts = explode('_', $not_cc_var);
    foreach ($parts as $part) {
        $CamelCase .= ucfirst($part);
    }
    return $CamelCase;
}

function dbg($arr) {
    echo "<pre>" . print_r($arr, 1) . "</pre>";
}

function nvl() {
    $args = func_get_args();
    foreach ($args as $arg) {
        if (!empty($arg)) return $arg;
    }
}

function decode() {
    $args = func_get_args();
    $exp = array_shift($args);
    $default = count($args) % 2 ? array_pop($args) : '';
    while (!empty($args)) {
        $value = array_shift($args);
        $dict[array_shift($args)] = $value;
    }
}

function form_error_handler($errno, $errstr, $errfile, $errline){
        global $ii_err;
        global $err;
        switch ($errno){
        case E_ERROR:
            $err[$ii_err++]= "<b>ERROR</b> [$errno] $errstr<br />\n"
                            ."  Fatal error on line $errline in file $errfile";
            break;
        case E_WARNING:
            $err[$ii_err++]= "<b>WARING:</b> [$errno] $errstr<br />\n";
            break;
        case E_NOTICE:
            $err[$ii_err++]= "<b>NOTICE</b> [$errno] $errstr<br />\n";
            break;
        default:
            $err[$ii_err++]= "Unknown error type: [$errno] $errstr<br />\n";
            break;
        }
        return true;
}
